package com.mit.web.config
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.xml.XMLDocument;
	import flash.xml.XMLNode;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ArrayUtil;


	public class SizeGroupConfig
	{
		private static var _me:SizeGroupConfig;
		private var _config:ArrayCollection;
		private var _loader:URLLoader;
		
		private const CONFIG:String = "com/mit/web/config/SizeGroupConfig.xml";
		public function SizeGroupConfig()
		{
			var u:URLRequest = new URLRequest(CONFIG);
			_loader = new URLLoader(u);
			_loader.addEventListener(Event.COMPLETE, xmlloaded);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, loadfault);
		}
		
		public static function getInstance():SizeGroupConfig{
			if(_me == null)
				_me = new SizeGroupConfig();
			return _me;
		}
		
		private function xmlloaded(event:Event):void{
			var xml:XMLDocument = new XMLDocument();
			xml.ignoreWhite = true;
			xml.parseXML(_loader.data);
			_config = new ArrayCollection();
			for(var i:int = 0 ; i < xml.firstChild.childNodes.length; i ++){
				var note:XMLNode = xml.firstChild.childNodes[i];
				if(note.attributes.active == "true"){
					var config:String = note.attributes.name;
					var sampleSize:String = note.attributes.sampleSize;
					var _sizeGroup:Array = new Array();
					var _props:Array = new Array();
					for(var j:int = 0; j < note.childNodes.length; j ++){
						var childNote:XMLNode = note.childNodes[j];
						_sizeGroup.push(childNote.attributes.name);						
						_props.push(childNote.attributes.datafield);
					}
					_config.addItem({name:config,sampleSize:sampleSize,sizeGroup:_sizeGroup,dataFields:_props});
				}
				
			}			
		}
		
		private function loadfault(event:Event):void{
			trace(event);
		}
		
		/**
		 * 返回指定名称的尺码组<br/>
		 * 例如 getSizeConfig("WOMAN_O1")，返回["S","M","L","F"]；<br/>
		 * 如果不存在，返回空Array对象
		 * */
		public function getSizeConfig(name:String):Array{
			for each(var o:Object in _config){
				if(o.name == name){
					return o.sizeGroup;	
				}
			}
			return new Array();
		}
		
		/**
		 * 返回指定名称的尺码组所对应的数据列名称<br/>
		 * 例如 getDataFields("WOMAN_O1")，返回["s01","s02","s03","s04"]；<br/>
		 * 如果不存在，返回空Array对象
		 * */
		public function getDataFields(name:String):Array{
			for each(var o:Object in _config){
				if(o.name == name){
					return o.dataFields;	
				}
			}
			return new Array();
		}
		
		/**
		 * 返回一个尺码组的默认打版的尺码
		 * */
		public function getSampleSize(name:String):String{
			for each(var o:Object in _config){
				if(o.name == name){
					return o.sampleSize;	
				}
			}
			return "";
		}
		
		/**
		 * 给定一个尺码组中某些尺码，返回对应的数据列的名称。
		 * <br/>
		 * 例如给S,M，返回 S01,S02
		 * */
		public function getSelectedDataFields(name:String, selectedSizes:ArrayCollection):ArrayCollection{
			var sizes:Array = getSizeConfig(name);
			var datas:Array = getDataFields(name);
			var rtn:ArrayCollection = new ArrayCollection();
			for each(var size:String in selectedSizes){
				var idx:int = sizes.indexOf(size);
				if(idx > -1)
					rtn.addItem(datas[idx]);
				
			}
			return rtn;
		}
		
		/**
		 * 根据数据列的名称返回对应尺码组中某些尺码。
		 * <br/>
		 * 例如给S01,S02，返回 S,M
		 * */
		public function getSelectedSizes(name:String, selectedDataFields:ArrayCollection):ArrayCollection{
			var sizes:Array = getSizeConfig(name);
			var datas:Array = getDataFields(name);
			var rtn:ArrayCollection = new ArrayCollection();
			for each(var field:String in selectedDataFields){
				var idx:int = datas.indexOf(field);
				if(idx > -1)
					rtn.addItem(sizes[idx]);
				
			}
			return rtn;
		}
	}
}